package com.handinfo.android.uicontrols.controls;

import android.graphics.Bitmap;
import android.graphics.PointF;
import android.view.MotionEvent;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.utils.Tools;

/**
 * 消息确认框
 * @author handinfo
 */
public class DWMessageBox extends DWControl
{
	private DWButton m_softkey_left = null;
	private DWButton m_softkey_right = null;
	private DWTextbox m_contents = null;
	private DWTitle m_title = null;
	private DWFrame m_frame = null;
	private DWBackground m_background = null;
	private byte m_result = 0;
	public static final byte SOFTKEY_OK = 1;
	public static final byte SOFTKEY_BACK = 2;

	public DWMessageBox(String title, String message)
	{
		this(title, message, "确定", "返回");
	}
	
	public DWMessageBox(String title, String message, String leftkey, String rightkey) {
		setShowWideHigh(DWControlsManager.CONTROL_WIDTH / 2, DWControlsManager.CONTROL_HEIGHT / 2);
		setPoint((DWGameManager.Screen_Width - m_show_w) >> 1, (DWGameManager.Screen_Height - m_show_h) >> 1);
		m_frame = new DWFrame(m_show_x, m_show_y, m_show_w, m_show_h);
		
		m_title = new DWTitle(title, m_frame);
		m_frame.addControl(m_title);
		
		Bitmap img = null;
		Bitmap img1 = null;
		
		if (rightkey != null || leftkey != null) {
			img = DWControlsManager.getInstance().createControlImage("anniu_6");
			img1 = DWControlsManager.getInstance().createControlImage("anniu_6ax");
		}
		
		m_background = new DWBackground(
				m_show_w - 20, m_show_h - m_title.getShowHeight());
		m_background.setFill(true);
		m_background.setNearAnchor(m_title, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM, 0, 10);
		m_frame.addControl(m_background);
		
		if (leftkey != null) {
			m_softkey_left = new DWButton(leftkey, img);
			m_softkey_left.setDownImage(img1);
			m_softkey_left.setNearAnchor(m_frame, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 15, -15);
			m_frame.addControl(m_softkey_left);
		}
		
		if (rightkey != null) {
			m_softkey_right = new DWButton(rightkey, img);
			m_softkey_right.setDownImage(img1);
			m_softkey_right.setNearAnchor(m_frame, Tools.RIGHT_BOTTOM, Tools.RIGHT_BOTTOM, -15, -15);
			m_frame.addControl(m_softkey_right);
		}
		
		m_contents = new DWTextbox(message, m_background.getShowWidth() - 60, m_background.getShowHeight() - 60);
		m_contents.setNearAnchor(m_background, Tools.LEFT_TOP, Tools.LEFT_TOP, 30, 20);
		m_frame.addControl(m_contents);
	}
	
	
	public DWMessageBox(String title, String message, String leftkey, String rightkey,boolean havebox) {
		setShowWideHigh(DWControlsManager.CONTROL_WIDTH / 2, DWControlsManager.CONTROL_HEIGHT / 2);
		setPoint((DWGameManager.Screen_Width - m_show_w) >> 1, (DWGameManager.Screen_Height - m_show_h) >> 1);
		m_frame = new DWFrame(m_show_x, m_show_y, m_show_w, m_show_h);
		
		m_title = new DWTitle(title, m_frame);
		m_frame.addControl(m_title);
		
		Bitmap img = null;
		Bitmap img1 = null;
		
		if (rightkey != null || leftkey != null) {
			img = DWControlsManager.getInstance().createControlImage("anniu_6");
			img1 = DWControlsManager.getInstance().createControlImage("anniu_6ax");
		}
		
		m_background = new DWBackground(
				m_show_w - 20, m_show_h - m_title.getShowHeight());
		m_background.setFill(true);
		m_background.setNearAnchor(m_title, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM, 0, 10);
		m_frame.addControl(m_background);
		
		if (leftkey != null) {
			m_softkey_left = new DWButton(leftkey, img);
			m_softkey_left.setDownImage(img1);
			m_softkey_left.setNearAnchor(m_frame, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 15, -15);
			m_frame.addControl(m_softkey_left);
		}
		
		if (rightkey != null) {
			m_softkey_right = new DWButton(rightkey, img);
			m_softkey_right.setDownImage(img1);
			m_softkey_right.setNearAnchor(m_frame, Tools.RIGHT_BOTTOM, Tools.RIGHT_BOTTOM, -15, -15);
			m_frame.addControl(m_softkey_right);
		}
		
		if(havebox){
			DWImageBox ib_tc = new DWImageBox(UIWindows.createImage("/img/newui/fanhuianniu_1.gnp"));
			ib_tc.setNearAnchor(m_title, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER, 0, 0);
			ib_tc.setTouchZoomIn(20, 20);
			ib_tc.addListener(new DWListener() {
				public void OnDoubleClick() {
				}
				public void OnClick() {
					DWControlsManager.getInstance().removeControl(m_frame);
				}
			});
			m_frame.addControl(ib_tc);
		}
		
		m_contents = new DWTextbox(message, m_background.getShowWidth() - 60, m_background.getShowHeight() - 60);
		m_contents.setNearAnchor(m_background, Tools.LEFT_TOP, Tools.LEFT_TOP, 30, 20);
		m_frame.addControl(m_contents);
	}

	public void render(DWGraphics g)
	{
		m_frame.render(g);
	}

	public void logic()
	{

	}
	
	public void refreshControl()
	{
		
	}

	public byte getResult()
	{
		return m_result;
	}
	
	public boolean doDown(PointF point)
	{
		if (m_softkey_left != null) {
			if (m_softkey_left.doDown(point)) {
				return true;
			}
		}
		if (m_softkey_right != null) {
			if (m_softkey_right.doDown(point)) {
				return true;
			}
		}
		return true;
	}
	
	public boolean doFling(PointF start, PointF end, float velocityX, float velocityY)
	{
		if (m_softkey_left != null) {
			if (m_softkey_left.doFling(start, end, velocityX, velocityY)) {
				m_result = SOFTKEY_OK;
				if (m_listener != null) {
					m_listener.OnClick();
				}
				return true;
			}
		}
		if (m_softkey_right != null) {
			if (m_softkey_right.doFling(start, end, velocityX, velocityY)) {
				m_result = SOFTKEY_BACK;
				if (m_listener != null) {
					m_listener.OnClick();
				}
				return true;
			}
		}
		return true;
	}
	
	public boolean doDrag(PointF start, PointF end, float velocityX, float velocityY)
	{
		if (m_softkey_left != null) {
			if (m_softkey_left.doDrag(start, end, velocityX, velocityY)) {
				m_result = SOFTKEY_OK;
				if (m_listener != null) {
					m_listener.OnClick();
				}
				return true;
			}
		}
		if (m_softkey_right != null) {
			if (m_softkey_right.doDrag(start, end, velocityX, velocityY)) {
				m_result = SOFTKEY_BACK;
				if (m_listener != null) {
					m_listener.OnClick();
				}
				return true;
			}
		}
		return true;
	}
	
	public boolean onTouch(MotionEvent event, PointF point) {
		if (m_frame.checkPointOnRect(point.x, point.y)) {
			return true;
		}
		return false;
	}

	public boolean doClick(float x, float y)
	{
		if (m_frame.checkPointOnRect(x, y)) {
			if (m_softkey_left != null) {
				if (m_softkey_left.checkPointOnRect(x, y)) {
					m_result = SOFTKEY_OK;
					if (m_listener != null) {
						m_listener.OnClick();
					}
					return true;
				}
			}
			if (m_softkey_right != null) {
				if (m_softkey_right.checkPointOnRect(x, y)) {
					m_result = SOFTKEY_BACK;
					if (m_listener != null) {
						m_listener.OnClick();
					}
					return true;
				}
			}
		} else {
//			destruct();
		}
		return true;
	}

}
